//
//  ASFieldCell.swift
//  SmoSky
//
//  Created by zhuxietong on 2018/7/25.
//  Copyright © 2018年 tianrun. All rights reserved.
//

import Foundation
import Eelay
import AsyncDisplayKit




open class ASFieldNode<FType:FieldView>:MoDisplayNode{
    
    public var fieldView = FType()
    open override func didLoad() {
        super.didLoad()
        view.addSubview(fieldView)
    }
    
    open override func layout() {
        super.layout()
        fieldView.frame = self.bounds
        fieldView.mo.model = mo.model
    }
}
open class ASFieldsCell: JoCellNode{
    open var fields = [MoDisplayNode]()
    
    open override func didLayoutRules() {
        super.didLayoutRules()
        backgroundColor = .white
        separatorColor = UIColor.separator_color
        separatorHeight = TP.separatorHeight
    }
    
    open override func mo_model_willSet(new: NSMutableDictionary) {
        var i = 0
        fields.forEach {[weak self] (field) in
            guard let ws = self else{return}
//            field.controller = ws.mo._controller
            field.mo.model = DataNode.path(node: "\(FK.fields).\(i)", new, value: NSMutableDictionary())

            i = i+1
        }
    }
    
   
}


open class ASFieldCell: JoCellNode {
    public let textF = ASFieldNode<FTextField>()
    open override func mo_model_reload() {
        textF.fieldView.controller = self.content_controller
        
        if let f = DataNode<NSMutableDictionary>.path(node: "\(FK.field)", mo.model)
        {
            textF.mo.model = f
        }
        else
        {
            if let f = DataNode<NSMutableDictionary>.path(node: "\(FK.fields).0", mo.model)
            {
                textF.mo.model = f
            }
        }
    }
    open override func specGetter() -> _Spec {
        return _Spec.inset([], [20], child: textF["40"])
    }
}

